/**
 * Created with IntelliJ IDEA.
 * Description:实例化对象的
 * User: mac
 * Date: 2022-09-08
 * Time: 18:16
 * 1.new
 * 2.
 */

//面试问题：你知道Cloneable接口吗？为啥这个接口是一个空接口？
//有啥作用？ 空接口->标志接口->代表当前这个类是可以被克隆的
//决定是深拷贝或者浅拷贝的不是方法，而是看你怎么实现
class Money implements Cloneable{
    public double m = 12.5;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{
    public int age;
    public Money money = new Money();

    public void eat() {
        System.out.println("吃！");
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person)super.clone();
        tmp.money = (Money) this.money.clone();
        return tmp;
    }
}
public class Clonable {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        Person person2 = (Person) person.clone();
        System.out.println(person.money.m);
        System.out.println(person2.money.m);
        System.out.println("===================");
        person2.money.m = 99.99;
        System.out.println(person.money.m);
        System.out.println(person2.money.m);
    }

    public static void main1(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        person.age = 99;
        Person person2 = (Person) person.clone();
        System.out.println(person2);

        System.out.println("===============");
        person2.age = 199;

        System.out.println(person);
        System.out.println(person2);
    }
}
